<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-${function}</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<!-- 页面内容 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 搜索条件 -->
            <form class="layui-form">
                <div class="layui-form-item">
                <#list fieldList as field>
                    <#if field.pageTableQuery>
                    <div class="layui-inline">
                        <label class="layui-form-label">${field.javaFieldComment}</label>
                        <div class="layui-input-inline">
                        <#if field.pageHtmlType == 'INPUT_NUMBER'>
                            <input type="number" name="${field.javaField}"autocomplete="off" class="layui-input" step="1" lay-affix="number">
                        <#elseif field.pageHtmlType == 'SELECT'>
                            <select name="${field.javaField}" th:with="dataList=${r'${@dictService.listData('}'${field.pageDictType}'${r')}'}">
                                <option value="">请选择</option>
                                <option th:each="dict : ${r'${dataList}'}" th:text="${r'${dict.dictItem}'}" th:value="${r'${dict.dictValue}'}"></option>
                            </select>
                        <#elseif field.pageHtmlType == 'CHECKBOX'>
                            <input type="checkbox" name="${field.javaField}" value="" title="选项1">
                        <#elseif field.pageHtmlType == 'RADIO'>
                            <#list dictTypeList as dictType>
                            <#if field.pageDictType == dictType.dictCode>
                            <#list dictType.dataList as dictData>
                            <input type="radio" name="${field.javaField}" value="${dictData.dictValue}" title="${dictData.dictItem}">
                            </#list>
                            <#else>
                            <input type="radio" name="${field.javaField}" value="" title="选项1">
                            </#if>
                            </#list>
                        <#elseif field.pageHtmlType == 'DATE'>
                            <input type="text" name="${field.javaField}" id="${field.javaField}" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                        <#elseif field.pageHtmlType == 'DATETIME'>
                            <input type="text" name="${field.javaField}" id="${field.javaField}" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                        <#elseif field.pageHtmlType == 'TIME'>
                            <input type="text" name="${field.javaField}" id="${field.javaField}" placeholder="HH:mm:ss" autocomplete="off" class="layui-input">
                        <#elseif field.pageHtmlType == 'YEAR'>
                            <input type="text" name="${field.javaField}" id="${field.javaField}" placeholder="yyyy" autocomplete="off" class="layui-input">
                        <#elseif field.pageHtmlType == 'SWITCH'>
                            <input type="checkbox" name="${field.javaField}" lay-skin="switch" lay-filter="${field.javaField}" value="true" title="ON|OFF">
                        <#else>
                            <input type="text" name="${field.javaField}" placeholder="请输入" class="layui-input">
                        </#if>
                        </div>
                    </div>
                    </#if>
                </#list>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-md" lay-submit lay-filter="tableSearch">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格工具 -->
<script type="text/html" id="tableToolbar">
    <div class="layui-btn-container">
        <#if genAdd>
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-addition"></i>新建
        </button>
        </#if>
        <#if genRemove>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
        </#if>
    </div>
</script>

<!-- 表格行工具 -->
<script type="text/html" id="tableRowTool">
    <#if genEdit>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
    </#if>
    <#if genRemove>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</a>
    </#if>
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['element', 'layer', 'table', 'laydate', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var laydate = layui.laydate;
        var form = layui.form;

<#list fieldList as field>
    <#if field.pageTableQuery>
        <#if field.pageHtmlType == 'DATETIME'>
        laydate.render({
            elem: '#${field.javaField}',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            fullPanel: true,
            <#if field.pageTableQueryType == 'BETWEEN'>
            range: true
            </#if>
        });
        <#elseif field.pageHtmlType == 'TIME'>
        laydate.render({
            elem: '#${field.javaField}',
            type: 'time',
            format: 'HH:mm:ss',
            <#if field.pageTableQueryType == 'BETWEEN'>
            range: true
            </#if>
        });
        <#elseif field.pageHtmlType == 'YEAR'>
        laydate.render({
            elem: '#${field.javaField}',
            type: 'year',
            format: 'yyyy',
            <#if field.pageTableQueryType == 'BETWEEN'>
            range: true
            </#if>
        });
        <#elseif field.pageHtmlType == 'DATE'>
        laydate.render({
            elem: '#${field.javaField}',
            type: 'date',
            format: 'yyyy-MM-dd',
            <#if field.pageTableQueryType == 'BETWEEN'>
            range: true
            </#if>
        });
        </#if>
    </#if>
</#list>

        // table渲染
        var tableObj = table.render({
            elem: '#dataTable',
            url: '/${entityName}/<#if genPageTableType == 'PAGE'>page<#else>list</#if>',
            method: 'GET',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            where: { // 添加额外条件

            },
            autoSort: false,
            toolbar: '#tableToolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {type: 'numbers', title: '序号', align: 'center'},
        <#list fieldList as field>
            <#if field.pageTable>
                <#if field.pageDictType??>
                    {
                        field: '${field.javaField}', title: '${field.javaFieldComment}', width: ${field.pageTableWidth}, align: 'center'<#if field.pageTableOrder?? && field.pageTableOrder == true>, sort: true</#if>,
                        templet: function (row) {
                            return row.${field.javaField}Desc;
                        }
                    },
                <#else>
                    <#if field.javaType == 'Boolean'>
                    {
                        field: '${field.javaField}', title: '${field.javaFieldComment}', width: ${field.pageTableWidth}, align: 'center'<#if field.pageTableOrder?? && field.pageTableOrder == true>, sort: true</#if>,
                        templet: function (row) {
                            return row.${field.javaField} === true ? '是' : '否';
                        }
                    },
                    <#else>
                    {field: '${field.javaField}', title: '${field.javaFieldComment}', width: ${field.pageTableWidth}, align: 'center'<#if field.pageTableOrder?? && field.pageTableOrder == true>, sort: true</#if>},
                    </#if>
                </#if>
            </#if>
        </#list>
                    <#if genEdit || genRemove>
                    {fixed: 'right', title: '操作', minWidth: 200, align: 'center', templet: '#tableRowTool'}
                    </#if>
                ]
            ],
            <#if genPageTableType == 'PAGE'>
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], // 自定义分页布局
                limit: 10,
                limits: [5, 10, 20, 50, 100],
                curr: 1,
                groups: 5, // 只显示5个连续页码
                first: false,
                last: false
            },
            <#else>
            page: false,
            </#if>
            loading: true,
            skin: 'grid', // 边框风格
            size: 'md', // 尺寸
            even: true, // 隔行背景
            before: function (options) { // 数据渲染之前的回调函数

            },
            done: function (res, curr, count, origin) { // 数据渲染完毕的回调函数

            },
            error: function (e, msg) { // 数据请求失败的回调函数

            },
            parseData: function (res) { // 数据格式解析的回调函数
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    <#if genPageTableType == 'PAGE'>
                    "count": res.data.total,
                    "data": res.data.results
                    <#else>
                    "data": res.data
                    </#if>
                }
            },
        });

        // 触发排序事件
        table.on('sort(dataTable)', function (obj) {
            table.reload('dataTable', {
                initSort: obj,
                where: {
                    prop: obj.field,
                    order: obj.type
                }
            });
        });

        // 监听表格工具条
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    <#if genPageFormOpenType == 'DIALOG'>
                    area: ['<#if genPageFormType == 'ONE'>500px<#elseif genPageFormType == 'TWO'>800px<#else>1200px</#if>', '450px'],
                    <#else>
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    area: ['<#if genPageFormType == 'ONE'>500px<#elseif genPageFormType == 'TWO'>800px<#else>1200px</#if>', '100%'],
                    </#if>
                    title: '新建${function}',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/${entityName}/addForm'
                });
            }
            if (obj.event === 'batchRemove') {
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请先选择数据', {icon: 0});
                    return;
                }
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    var ids = checkRows.data.map(function (row) {
                        <#list fieldList as field>
                        <#if field.columnPk>
                        return row.${field.javaField};
                        </#if>
                        </#list>
                    });
                    $.ajax({
                        url: '/${entityName}/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: ids}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听表格行工具
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    <#if genPageFormOpenType == 'DIALOG'>
                    area: ['<#if genPageFormType == 'ONE'>500px<#elseif genPageFormType == 'TWO'>800px<#else>1200px</#if>', '450px'],
                    <#else>
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    area: ['<#if genPageFormType == 'ONE'>500px<#elseif genPageFormType == 'TWO'>800px<#else>1200px</#if>', '100%'],
                    </#if>
                    title: '编辑${function}',
                    shadeClose: true,
                    scrollbar: false,
                    <#list fieldList as field>
                    <#if field.columnPk>
                    content: '/${entityName}/editForm/' + obj.data.${field.javaField}
                    </#if>
                    </#list>
                });
            }
            if (obj.event === 'remove') {
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    $.ajax({
                        url: '/${entityName}/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        <#list fieldList as field>
                        <#if field.columnPk>
                        data: JSON.stringify({idList: [obj.data.${field.javaField}]}),
                        </#if>
                        </#list>
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听查询
        form.on('submit(tableSearch)', function (data) {
            tableObj.reload({<#if genPageTableType == 'PAGE'>page: {curr: 1}, </#if>where: data.field});
            return false;
        });
    });
</script>
</html>